package com.yili.oa.service.impl;

import com.yili.oa.dao.DealersLevelMapper;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.models.DealersLevel;
import com.yili.oa.models.DealersLevelExample;
import com.yili.oa.service.DealersLevelService;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.utils.annotate.DataBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@DataBase("db_manage")
@Transactional(value = "transactionManager")
public class DealersLevelServiceImpl implements DealersLevelService {

    @Autowired
    DealersLevelMapper mapper;


    @Override
    public List<DealersLevel> get(SearchBase searchBase) {
        DealersLevelExample example = new DealersLevelExample();
        DealersLevelExample.Criteria criteria = example.createCriteria();
        if(searchBase.getId()!=null){
            criteria.andIdEqualTo(Long.valueOf(searchBase.getId()));
        }
        return mapper.selectByExample(example);
    }

    @Override
    public int add(DealersLevel dealersLevel) {
        Long time = new Date().getTime();
        dealersLevel.setCreateTime(time);
        return mapper.insert(dealersLevel);
    }

    @Override
    public int update(DealersLevel dealersLevel) {
        return mapper.updateByPrimaryKeySelective(dealersLevel);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            mapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public DealersLevel getById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DealersLevel getNextLevel(Long id) {
        DealersLevel dealersLevel = getById(id);
        DealersLevelExample example = new DealersLevelExample();
        DealersLevelExample.Criteria criteria = example.createCriteria();
        criteria.andNecessaryExperienceGreaterThan(dealersLevel.getNecessaryExperience());
        example.setOrderByClause("necessary_experience");
        List<DealersLevel> dealersLevels = mapper.selectByExample(example);
        if(ListUtils.isNotNullOrEmpty(dealersLevels)){
            return dealersLevels.get(0);
        }
        return null;
    }
}
